﻿Imports Training.Common
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Drawing.Imaging

Partial Public Class tester
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim aString As String
        aString = "uid=22&qid=1"
        hypEnc.NavigateUrl = "Test2.aspx?data=" & Encrypt(aString)

        Dim ref As String
        Dim aRef As Array
        ref = HttpContext.Current.Request.UrlReferrer.ToString
        ref = Replace(ref, "http://", "")
        aRef = Split(ref, "/")


        lblRef.Text = "You are with " & aRef(0)
    End Sub

    ''' <summary>
    ''' Get number of questions correct needed to pass a quiz
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnQuizPassing_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuizPassing.Click
        lblPassing.Text = GetPassingPercentage(Trim(tbQuizID.Text))
    End Sub

    ''' <summary>
    ''' Create Certificate of completion
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnImager_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImager.Click
        Dim path As String = Server.MapPath("/images/") ' path to images directory
        Dim oBitmap As Bitmap = New Bitmap(path & "cert.bmp")
        Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
        Dim oColor As System.Drawing.Color

        ' Users Name
        Dim sText As String = tbName.Text
        Dim sFont As String = "Times New Roman"

        Dim oBrush As New SolidBrush(oColor)
        Dim oBrushWrite As New SolidBrush(Color.Red)

        Dim oFont As New Font(sFont, 36)
        Dim oPoint As New PointF(600, 430)

        ' Date of Completion
        Dim dText As String = FormatDateTime(Now(), DateFormat.ShortDate)

        Dim dBrush As New SolidBrush(oColor)
        Dim dBrushWrite As New SolidBrush(Color.Black)

        Dim dFont As New Font("Arial", 19)
        Dim dPoint As New PointF(820, 708)

        ' name of quiz completed
        Dim cText As String = "Completed iScreen OFD Training"

        Dim cBrush As New SolidBrush(oColor)
        Dim cBrushWrite As New SolidBrush(Color.DarkBlue)

        Dim cFont As New Font("Arial", 32)
        Dim cPoint As New PointF(600, 480)

        ' graphic
        oGraphic.FillRectangle(oBrush, 0, 0, 468, 60)
        oGraphic.TextRenderingHint = TextRenderingHint.AntiAlias

        ' alignment
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center

        ' generate text on graphic
        oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint, sf) ' username
        oGraphic.DrawString(dText, dFont, dBrushWrite, dPoint) ' date
        oGraphic.DrawString(cText, cFont, cBrushWrite, cPoint, sf) ' completed quiz

        ' save certificate image
        ' ** NOTE: REPLACE tbName with UserID and QuizID ex. 1_1_cert_img.jpg
        Dim cImage As String = Replace(tbName.Text, " ", "_") & "_cert_img.jpg"
        cImage = LCase(cImage)
        cImage = Replace(cImage, "'", "")
        oBitmap.Save(Server.MapPath("/certs/" & cImage), ImageFormat.Jpeg)

        Response.Write("View the generated image <a target=""_blank"" href=""certs/" & cImage & """>here</a>")
    End Sub
End Class